< previous page page_296 next page >

Page 296
Note that the For . . . Next loop counts from 0 to 3, not 1 to 4 because the array elements, by default, are numbered beginning with 0.
Removing Elements from a Control Array
While you are in the design environment, you can remove control array elements from a form in several ways:
By selecting the control element and pressing the Delete key
By selecting it and choosing Edit, Delete from the menu system
By right-clicking it and selecting Delete from the context menu
This way, you can clear the control from the form and reduce the number of elements of the control array. If, for some reason, you want to return the elements of the control array to individual controls that are not part of the array, you need to specify new Name property values for all but one of the controls and then delete the Index property values of the controls to return them to null values.
CAUTION
When you remove a control from a control array, the rest of the controls are not automatically renumbered. Therefore, deleting controls can leave gaps in the Index values of the array's remaining elements. These gaps can cause problems if, for example, you have code that uses a For loop to work with the elements of the control array. In this particular case, an error occurs when the loop tries to access a control array element via an Index property value that has been deleted.

Working with Control Arrays
Now that you understand the mechanics behind creating control arrays, you will learn some ways that they are commonly used. In general, you group controls into an array when you need to use similar code to process each control, such as four text boxes representing lines in a street address. Simplifying the programming of event procedures is another reason to use a control array. For example, if you have 26 command buttons that represent the letters of the alphabet, it is easier to deal with them as an array than as separate controls.
Using a Control Array in Your Programs
There are a number of uses for control arrays in your programs. The example of simultaneously converting the contents of all text boxes to uppercase is one.
Another common use of control arrays is to find which option button in an array is selected. Because only one option button in a group can be selected at any one time, you can use a For loop to determine the value, as illustrated in Listing 13.1.

 
< previous page page_296 next page >